package com.mhkj.revise.function.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.common.enums.PlanType;
import com.mhkj.revise.function.service.FunctionPlanService;
import com.mhkj.revise.plan.entity.Plan;
import com.mics.core.common.componet.JsonHandler;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.common.exception.ErrorCode;
import com.mics.core.common.exception.ParamException;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.rbac.entity.Dictionary;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 职能计划
 * @author mics
 * @date 2018年8月29日
 * @version 1.0
 */
@Controller
@RequestMapping("/functionPlan")
public class FunctionPlanController extends BaseController<Plan>{
	@Resource
	private FunctionPlanService functionPlanService;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	private JsonHandler jsonHandler;
	@Resource
	public void setFunctionPlanService(FunctionPlanService functionPlanService) {
		super.setBaseService(functionPlanService);
	}


	@RequestMapping("/listByPage")
	@ResponseBody
	public void listByPage(HttpServletResponse response,Plan plan,int pageIndex,int pageSize){
		Pagination<Plan> pagination =  functionPlanService.listByPage(plan,pageIndex,pageSize);
		jsonHandler.sendJsonString(response, true, "获取数据成功", pagination, new String[]{
			"id","name","functionOffice.name","beginDate","endDate","createDate","state","auditDate","auditRemark"}, 
			"id","name","functionOfficeValue","beginDate","endDate","createDate","state","auditDate","auditRemark");
	}

	
	@RequestMapping("/save")
	@ResponseBody
	public void save(HttpServletResponse response,Plan plan) {
		if(plan == null){
			throw new ParamException(ErrorCode.PARAM_ERROR,"参数错误！");
		}
		//保存计划类型
		Dictionary planType = dictionaryService.getByCode(PlanType.FUNC_TYPE.getCode());
		plan.setPlanType(planType);
		String message =  functionPlanService.save(plan);
		if(StringUtils.hasText(message)){
			jsonHandler.sendJsonSimple(response, false, message);
		}else{
		    plan = functionPlanService.getById(plan.getId());
			jsonHandler.sendJsonString(response, true, "获取数据成功", plan, new String[]{"id"}, "planId");
		}
	}

	@Override
	public ResponseMessage del(Plan t) {
		return super.del(t);
	}
	
	/**
	 * 申请审核
	 * @author mics
	 * @date 2018年8月15日
	 * @version 1.0
	 */
	@RequestMapping("/audit")
	@ResponseBody
	public ResponseMessage audit(HttpServletResponse response,long planId){
		Plan plan = functionPlanService.getById(planId);
		plan.setState(1);
		functionPlanService.edit(plan);
		return ResponseMessage.createMessage(true, "已经提交审核");
	}
	

	
}
